home *** CD-ROM | disk | FTP | other *** search
/ Programming in Microsoft Windows with C# / Programacion en Microsoft Windows con C#.iso / Original Code / Clip Drag and Drop / ClipView / ClipView.cs next >
Encoding:
Text File  |  2001-01-15  |  5.7 KB  |  170 lines

  1. //---------------------------------------
  2. // ClipView.cs ⌐ 2001 by Charles Petzold
  3. //---------------------------------------
  4. using System;
  5. using System.Drawing;
  6. using System.Drawing.Imaging;
  7. using System.IO;
  8. using System.Windows.Forms;
  9.  
  10. class ClipView: Form
  11. {
  12.      string[] astrFormats = 
  13.      { 
  14.      DataFormats.Bitmap, DataFormats.CommaSeparatedValue, DataFormats.Dib,
  15.      DataFormats.Dif, DataFormats.EnhancedMetafile, DataFormats.FileDrop, 
  16.      DataFormats.Html, DataFormats.Locale, DataFormats.MetafilePict, 
  17.      DataFormats.OemText, DataFormats.Palette, DataFormats.PenData, 
  18.      DataFormats.Riff, DataFormats.Rtf, DataFormats.Serializable, 
  19.      DataFormats.StringFormat, DataFormats.SymbolicLink, DataFormats.Text, 
  20.      DataFormats.Tiff, DataFormats.UnicodeText, DataFormats.WaveAudio 
  21.      };
  22.      
  23.      Panel         panelDisplay;
  24.      RadioButton[] aradio;
  25.      RadioButton   radioChecked;
  26.  
  27.      public static void Main()
  28.      {
  29.           Application.Run(new ClipView());
  30.      }
  31.      public ClipView()
  32.      {
  33.           Text = "Clipboard Viewer";
  34.  
  35.                // Create variable-width panel for clipboard display.
  36.  
  37.           panelDisplay = new Panel();
  38.           panelDisplay.Parent = this;
  39.           panelDisplay.Dock = DockStyle.Fill;
  40.           panelDisplay.Paint += new PaintEventHandler(PanelOnPaint);
  41.           panelDisplay.BorderStyle = BorderStyle.Fixed3D;
  42.  
  43.                // Create splitter.
  44.  
  45.           Splitter split = new Splitter();
  46.           split.Parent = this;
  47.           split.Dock = DockStyle.Left;
  48.  
  49.                // Create panel for radio buttons.
  50.  
  51.           Panel panel = new Panel();
  52.           panel.Parent = this;
  53.           panel.Dock = DockStyle.Left;
  54.           panel.Width = 200;
  55.  
  56.                // Create radio buttons.
  57.           
  58.           aradio = new RadioButton[astrFormats.Length];
  59.           EventHandler eh = new EventHandler(RadioButtonOnClick);
  60.  
  61.           for (int i = 0; i < astrFormats.Length; i++)
  62.           {
  63.                aradio[i] = new RadioButton();
  64.                aradio[i].Parent = panel;
  65.                aradio[i].Location = new Point(4, 12 * i);
  66.                aradio[i].Size = new Size(300, 12);
  67.                aradio[i].Click += eh;
  68.                aradio[i].Tag = astrFormats[i];
  69.           }
  70.                // Set autoscale base size.
  71.  
  72.           AutoScaleBaseSize = new Size(4, 8);
  73.  
  74.                // Set time for 1 second.
  75.  
  76.           Timer timer = new Timer();
  77.           timer.Interval = 1000;
  78.           timer.Tick += new EventHandler(TimerOnTick);
  79.           timer.Enabled = true;
  80.      }
  81.      void TimerOnTick(object obj, EventArgs ea)
  82.      {
  83.           IDataObject data = Clipboard.GetDataObject();
  84.  
  85.           for (int i = 0; i < astrFormats.Length; i++)
  86.           {
  87.                aradio[i].Text = astrFormats[i];
  88.                aradio[i].Enabled = data.GetDataPresent(astrFormats[i]);
  89.  
  90.                if (aradio[i].Enabled)
  91.                {
  92.                     if (!data.GetDataPresent(astrFormats[i], false))
  93.                          aradio[i].Text += "*";
  94.  
  95.                     object objClip = data.GetData(astrFormats[i]);
  96.  
  97.                     try
  98.                     {
  99.                          aradio[i].Text += " (" + objClip.GetType() + ")";
  100.                     }
  101.                     catch
  102.                     {
  103.                          aradio[i].Text += " (Exception on GetType!)";
  104.                     }
  105.                }
  106.           }
  107.           panelDisplay.Invalidate();
  108.      }
  109.      void RadioButtonOnClick(object obj, EventArgs ea)
  110.      {
  111.           radioChecked = (RadioButton) obj;
  112.           panelDisplay.Invalidate();
  113.      }
  114.      void PanelOnPaint(object obj, PaintEventArgs pea)
  115.      {
  116.           Panel    panel = (Panel) obj;
  117.           Graphics grfx  = pea.Graphics;
  118.           Brush    brush = new SolidBrush(panel.ForeColor);
  119.  
  120.           if (radioChecked == null || !radioChecked.Enabled)
  121.                return;
  122.  
  123.           IDataObject data = Clipboard.GetDataObject();
  124.  
  125.           object objClip = data.GetData((string) radioChecked.Tag);
  126.  
  127.           if (objClip == null)
  128.                return;
  129.  
  130.           else if (objClip.GetType() == typeof(string))
  131.           {
  132.                grfx.DrawString((string)objClip, Font, brush, 
  133.                                panel.ClientRectangle);
  134.           }
  135.           else if (objClip.GetType() == typeof(string[]))   // FileDrop
  136.           {
  137.                string str = string.Join("\r\n", (string[]) objClip);
  138.  
  139.                grfx.DrawString(str, Font, brush, panel.ClientRectangle);
  140.           }
  141.           else if (objClip.GetType() == typeof(Bitmap) ||
  142.                    objClip.GetType() == typeof(Metafile) ||
  143.                    objClip.GetType() == typeof(Image))
  144.           {
  145.                grfx.DrawImage((Image)objClip, 0, 0);
  146.           }
  147.           else if (objClip.GetType() == typeof(MemoryStream))
  148.           {
  149.                Stream stream = (Stream) objClip;
  150.                byte[] abyBuffer = new byte[16];
  151.                long   lAddress = 0;
  152.                int    iCount;
  153.                Font   font = new Font(FontFamily.GenericMonospace, 
  154.                                     Font.SizeInPoints);
  155.                float  y = 0;
  156.  
  157.                while ((iCount = stream.Read(abyBuffer, 0, 16)) > 0)
  158.                {
  159.                     string str = HexDump.ComposeLine(lAddress, abyBuffer, 
  160.                                                                iCount);
  161.                     grfx.DrawString(str, font, brush, 0, y);
  162.                     lAddress += 16;
  163.                     y += font.GetHeight(grfx);
  164.  
  165.                     if (y > panel.Bottom)
  166.                          break;
  167.                }
  168.           }
  169.      }
  170. }